﻿if (!!window.DD_belatedPNG) {
    DD_belatedPNG.fix(".slide img");
};

$(document).ready(function () {
    setInterval("slide($('#J_slide_list'),25,500)", 1500);
    var J_light_form = $("#J_light_form");
    var J_prize = $("#J_prize");
    var J_form_orderid = $("#J_form_orderid");
    var J_form_mobilenum = $("#J_form_mobilenum");
    var J_n = J_prize.find(".slide").eq(0).find(".slide-list").children().length - 1;
    var r1 = Math.floor(Math.random() * J_n);
    J_prize.find(".slide").eq(0).attr("data-eq", r1).find(".slide-list").css({ top: -r1 * 146 });
    var J_prize1 = setInterval("slide($('#J_prize').find('.slide').eq(0),146,500)", 1500);
    //抽奖
    $("#J_draw").click(function () {
        var show = $("#J_prize");
        //显示表单
        J_light_form.show();
        J_light_form.find(".light-bg").css("height", $(document).height());
        J_light_form.find(".light-box").css({ left: ($(window).width() - J_light_form.find(".light-box").width()) / 2, top: ($(window).height() - J_light_form.find(".light-box").height()) / 2 + $(document).scrollTop() });
        J_light_form.find(".light-iframe").css("height", $(document).height());
        //如果在抽奖中
        if ($(this).attr("data") == "draw") {
            return;
        };
        $(this).attr("data", "draw");
    }).attr("href", "javascript:void(0)");

    //验证填写的信息
    $("#J_form input").blur(function () {
        var t = $(this);
        var index = $.inArray(this, $.makeArray($("#J_form input")));
        var reg = [/^[\S*]{2,20}$/, /^(((13[0-9]{1})|(15[0-35-9]{1})|(18[05-9]{1}))+\d{8})$/];
        var val = t.val().replace(/(^\s*)|(\s*$)/g, "");
        var result = (!reg[index].exec(val)) ? "form-showerror" : "form-showsuccess";
        t.nextAll(".form-message").removeClass("form-showerror").removeClass("form-showsuccess").addClass(result);
    });

    $("#J_form").submit(function () {
        var data = {};
        data["r"] = Math.random();
        //检测表单状态
        var t = $(this);
        var fields = t.find("input:visible");
        var count = fields.length;
        for (var i = 0; i < count; i++) {
            field = fields.eq(i);
            if (field.nextAll(".form-showsuccess").length < 1) {
                field.focus();
                field.blur();
            };
            data[field.attr("name")] = field.val();
        };
        if (t.find(".form-showerror").length > 0) {
            return false;
        };
        //检测通过，隐藏表单
        J_light_form.hide();
        //开始抽奖
        clearInterval(J_prize1);
        //速度随机滚动
        s1 = Math.random() * 200; s1 += s1 < 65 ? 26 : 0;
        lt1 = setInterval("play($('#J_prize').find('.slide').eq(0),146," + s1 + ")", s1);
        //ajax提交
        var url = t.attr("action");
        $.ajax({
            async: true,
            type: "POST",
            url: url,
            data: data,
            dataType: "text",
            success: function (msg) {
                $("#J_draw").removeAttr("data");
                var data = eval("(" + msg + ")");
                var award = data.awardRes;
                $("#J_form_orderid").val("").next(".form-message").removeClass("form-showsuccess");
                if (award[4] > -1) { $("#J_lucky_left").html(award[4]); };
                if (award[5] > -1) { $("#J_lucky_win").html(award[5]); };                
                setTimeout("clearInterval(lt1); ", 2000);
                setTimeout("lottery_award([\"" + award[0] + "\",\"" + award[1] + "\",\"" + award[2] + "\",\"" + award[3] + "\"])", 2500);
                return false;
            },
            error: function () {
                $("#J_draw").removeAttr("data");
                /*停止滚动 不显示中奖*/
                setTimeout("clearInterval(lt1); ", 2000);
                setTimeout("lottery_award([\"error\", \"抱歉，出错了！请刷新页面后再试！\", \"\",-3])", 2500);
                return false;
            }
        });
        return false;
    });

    //提示关闭
    $("#J_light .light-close, #J_light_form .light-close").click(function () {
        $("#J_light, #J_light_form").hide();
    });
    $("#J_light a.confirm").click(function () {
        $("#J_light").hide();
    });
});
//轮播
function slide(element, step, time) {
    var slide_list = element.find(".slide-list");
    var slide_triggers = element.find(".slide-triggers");
    var count = slide_list.children().length;
    if ((count < 2) || (slide_list.height() < element.height() + step)) { return; }
    var slide_list_first = slide_list.children().eq(0);
    var eq = parseInt(element.attr("data-eq"));
    if (isNaN(eq)) { eq = -1; element.attr("data-eq", "0"); };
    eq += 1;
    if (eq > count - 1) {
        eq = 0;
        slide_list.animate({ top: -count * step }, time, function () {
            slide_list.animate({ top: 0 }, 0);
            slide_list.children().removeAttr("style");
        });
    } else {
        slide_list.animate({ top: -eq * step }, time);
    }
    if (slide_list.height() - count * step < element.height()) {
        for (var i = 0; i < eq - 1; i++) {
            slide_list.children().eq(i).css({ "position": "relative", "top": (count * step) + "px" });
        };
    };
    element.attr("data-eq", eq);
    slide_triggers.children().removeClass("slide-current").eq(eq).addClass("slide-current");
};
//滚动
function play(element, step, speed) {
    var eq = parseInt(element.attr("data-eq"));
    if (isNaN(eq)) { eq = 0; element.find(".slide-list").animate({ top: 0 }, 0); element.find("li:first").removeAttr("style"); };
    var count = element.find(".slide-list").children().length;
    eq += 1;
    if (eq > count - 1) {
        element.find("li:first").css({ "position": "relative", "top": (count * step) + "px" });
        element.find(".slide-list").animate({ top: -count * step }, speed, function () {
            element.find(".slide-list").animate({ top: 0 }, 0);
            element.find("li:first").removeAttr("style");
        });
        eq = 0;
        element.attr("data-eq", eq);
        return;
    }
    element.find(".slide-list").animate({ top: -eq * step }, speed);
    element.attr("data-eq", eq);
    return;
};
//滚动结束处理
function play_end(element, step, speed, top) {
    var eq = parseInt(element.attr("data-eq"));
    var count = element.find(".slide-list").children().length;
    if (eq > top) {
        element.find("li:first").css({ "position": "relative", "top": (count * step) + "px" });
        element.find(".slide-list").animate({ top: -count * step }, speed, function () {
            element.find(".slide-list").animate({ top: 0 }, 0);
            element.find("li:first").removeAttr("style");
            element.attr("data-eq", awards).find(".slide-list").animate({ top: -146 * awards }, speed).children().removeAttr("style");
        });
        return;
    };
    element.attr("data-eq", awards).find(".slide-list").animate({ top: -146 * awards }, speed * 2).children().removeAttr("style");
    return;
};
//显示弹层
function lightshow(awardres) {
    $("#J_draw").removeAttr("data");
    //弹层处理
    var light = $("#J_light");
    light.show();
    light.find(".light-bg").css("height", $(document).height());
    light.find(".light-box").css({ left: ($(window).width() - light.find(".light-box").width()) / 2, top: ($(window).height() - light.find(".light-box").height()) / 2 + $(document).scrollTop() });
    light.find(".light-iframe").css("height", $(document).height());
    light.find(".message").removeClass("attention success error");
    light.find(".message").addClass(awardres[0]).find("dl").addClass(awardres[0]).find("dt").html(awardres[1]).next("dd").html(awardres[2]);
};
//中奖处理
function lottery_award(awardres) {
    var award = awardres[3];
    var show = $("#J_prize");
    var J_n = show.find(".slide").eq(0).find(".slide-list").children().length - 1;
    if (award > 0) {
        var href = (award == 9) ? "javascript:void(0)" : $("#J_light a.confirm").attr("data");
        $("#J_light a.confirm").attr("href", href);
        awards = award - 1;
        play_end(show.find(".slide").eq(0), 146, 100, awards);
        setTimeout("$('#J_prize').find('.slide').find('.slide-list').animate({ top: -146 * " + awards + " }, 200)", 200);
        setTimeout("$('#J_prize').find('.slide').find('.slide-list').animate({ top: -146 * " + awards + " }, 200)", 1000);
    } else {
        s1 = Math.random() * J_n; s1 = Math.ceil(s1);
        t1 = show.find(".slide").eq(0);
        setTimeout("$('#J_prize').find('.slide-list').each(function() {  var t = $(this).parent().attr('data-eq') * -146; $(this).animate({top:t},500); })", 200);
    };
    setTimeout("lightshow([\"" + awardres[0] + "\",\"" + awardres[1] + "\",\"" + awardres[2] + "\",\"" + awardres[3] + "\"])", 1500);
};
//手机号验证
function verifyMobile() { 
    
}
//提示
var awardcontent = {
    "award-1": ["error", "抱歉，出错了！请刷新页面后再试。", "", -1, -1],
    "award-2": ["error", "抱歉，订单号错误。", "", -1, -1],
    "award-3": ["error", "抱歉，手机号错误。", "", -1, -1],
    "award0": ["error", "很遗憾，今天您的运气不够，明天再来一定能中哦。", "",-1,-1],
    "award1": ["success", "恭喜您获得30元手机充值卡", "卡在1~3个工作日内打入您的通行证账户。","",100,10],
    "award2": ["success", "您幸运了，可以再要一次哦！", 200,5]
};